

function createHeader() {
  let token = uni.getStorageSync('token');
  // let token = 'starx#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTAwOTEyOTMsImlzcyI6Im1pbmkiLCJpZCI6MTMyMTcwMDE5MjI4NTYyMjI3MiwidWlkIjoiMzUzIiwidXNlcl9uYW1lIjoiIiwibmFtZSI6IiIsImVtYWlsIjoiIiwicGhvbmUiOiIiLCJuaWNrbmFtZSI6IumDkV4gXiIsImhlYWRpbWciOiJodHRwOi8vdGhpcmR3eC5xbG9nby5jbi9tbW9wZW4veHRiVjA5VklLaWJDazJzVlhWeXV5OHVuS2hvSEVXUVpDUVFvaWN3RXVybzdDdENiWHhjc2t5bHhWaWF0NG5iaWJMWTVmRldmS3BVRjRwZVVWM3hDNXhwbmxtd3NXc0JqQk1rSy8xMzIiLCJnb29nbGVfYXV0aCI6IiIsIkxldmVsX2NvZGUiOiIxIiwicm9sZV9pZCI6bnVsbCwicGVybWlzc2lvbl9pZHMiOm51bGx9.PsxPgZ_5W5ypFVHqApNVGxBKll7C_kSWQzjSBz9aGFU';
  return {
    'content-type': 'application/json',
    'Access-Token-OM': token ? token : ''
  };
}


module.exports = {
  get: (url, data = {}) => {
    // 获取应用实例
    return new Promise((resolve, reject) => {
      console.log('发送GET请求:' + url);
      console.log(data);
      const requestTask = uni.request({
        url,
        data,
        header: {
          'Access-Token-OM': uni.getStorageSync('token'),
          // 'Access-Token':'starx#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTA0MzM2NTEsImlzcyI6Im1pbmkiLCJpZCI6MTI4MDUzMTEzMzgwODc3NTE2OCwidWlkIjoiNCIsInVzZXJfbmFtZSI6IiIsIm5hbWUiOiIiLCJlbWFpbCI6IiIsInBob25lIjoiIiwibmlja25hbWUiOiLmmJ_nqboiLCJoZWFkaW1nIjoiaHR0cHM6Ly90aGlyZHd4LnFsb2dvLmNuL21tb3Blbi92aV8zMi9RMGo0VHdHVGZUTHpaa3N2OXRLdG8zY01aRXlIT3M2V2ljQnAxaFRaMWNudlZuSU9UVG5wWkhNOVNmdUFvWnFBVFF5M0lpYWlhVmF3MkUyOWtjcWtHVExhdy8xMzIiLCJnb29nbGVfYXV0aCI6IiIsIkxldmVsX2NvZGUiOiIzIiwicm9sZV9pZCI6WzEwMDAwMDAxXSwicGVybWlzc2lvbl9pZHMiOlsiZGFzaGJvYXJkIiwid3hfYm90IiwiZ29vZHNfbWdyIiwidGFibGUiLCJmb3JtIl19.TOUnnaFMExpYF69v3vqFCmIf5fdamo9bnincRm7vZzo',
          'content-type': 'application/json'
        },
        success: res => {
          console.log(`${url} 返回结果`);
          console.log(res);

          if (res.statusCode === 200) {
            if (res.data.code == 0) {
              resolve(res.data);
            } else if (res.data.code == 1) {
              resolve(res.data);
            } else {
              let access_token = uni.getStorageSync('token');
              log.error('请求 ' + url + ' 异常;' + ' 请求参数:' + JSON.stringify(data) + ';返回数据:' + JSON.stringify(res) + ' 用户token:' + access_token);
              log.error('请求 ' + url + ' 异常;' + ' 请求参数:', data, ';返回数据:', res, ' 用户token:', access_token);
              uni.hideLoading();
              uni.showModal({
                title: '提示',
                content: res.data.msg,
                showCancel: false,

                success(res) {
                  if (res.confirm) {// resolve(res.data);
                  } else if (res.cancel) {
                    console.log('用户点击取消');
                  }
                }

              });
            }
          } else if (res.statusCode === 401) {
            getCode();
          } else {
            let access_token = uni.getStorageSync('token');
            console.log('请求 ' + url + ' 异常;' + ' 请求参数:' + JSON.stringify(data) + ';返回数据:' + JSON.stringify(res) + ' 用户token:' + access_token);
            log.error('请求 ' + url + ' 异常;' + ' 请求参数:' + JSON.stringify(data) + ';返回数据:' + JSON.stringify(res) + ' 用户token:' + access_token);
            uni.showModal({
              title: '提示',
              content: res.errMsg,
              showCancel: false,
              complete: () => {
                reject(res.errMsg);
              },

              success(res) {
                if (res.confirm) {
                  uni.navigateBack({
                    delta: 1
                  });
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }

            });
            uni.hideLoading();
          }
        },
        fail: err => {
          uni.showModal({
            title: '提示',
            content: err,
            showCancel: false,
            complete: () => {
              reject(err);
            },

            success(res) {
              if (res.confirm) {
                uni.navigateBack({
                  delta: 1
                });
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }

          });
          uni.hideLoading();
        }
      }); // 取消请求任务
      // requestTask.abort();
    });
  },
  post: (url, data, method = 'POST') => {
    // 获取应用实例
    return new Promise((resolve, reject) => {
      console.log('发送POST请求:' + url);
      console.log(data);
      const requestTask = uni.request({
        method,
        data,
        url,
        header: createHeader('content-type', 'Access-Token'),
        // header:{
        //   'Access-Token': 'starx#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MTYzODE3NjAsImlzcyI6IjEiLCJpZCI6MSwidWlkIjoiIiwidXNlcl9uYW1lIjoieGluZ3BzaCIsIm5hbWUiOiLmmJ_lk4HnlJ_mtLsiLCJlbWFpbCI6bnVsbCwicGhvbmUiOiIxODYwNjIwMzcyOCIsIm5pY2tuYW1lIjoiIiwiaGVhZGltZyI6Imh0dHA6Ly9pbWcueGluZ2tvbmdsaWFuLm5ldC9pbWcvbG9nb18xXzI2MC5wbmciLCJnb29nbGVfYXV0aCI6IiIsIkxldmVsX2NvZGUiOiIiLCJwZXJtaXNzaW9uX2lkcyI6WyJzaG9wY291cG9uIiwic2hvcGNvdXBvbm1hbmFnZW1lbnQiLCJub2JpbmRpbmciLCJtdXN0c2VlIiwidXNlcm1hbmFnZW1lbnQiLCJ3aXRoZHJhd2FsYXVkaXQiLCJzZXR0bGVtZW50cmVjb3JkcyIsInJlYmF0ZW9yZGVyIiwiZ3Jvd3RodGFzayIsImFuYWx5c2lzcGFnZSIsImdvb2Rzc2Nob29sIiwib3BlcmF0aW9ucyIsInd4X2JvdCIsImdvb2Rzc2VsZWN0IiwiYmFzaXNwYWdlIiwiY3V0cHJpY2VfYXV0byIsImdvb2RzYXVkaXQiLCJzY21saXN0IiwibWFudWFsY29tbWlzc2lvbiIsInZpZGVvZ2lmc2hvdyIsImFuYWx5emVkYXRhIiwid2VsY29tZSIsInByb21vdGVjb21taXNzaW9uZXJyb3IiLCJTZWFyY2hfZGllX2ZhbnMiLCJtZXJjaGFudHNhdWRpdCIsImNhY2hlZSIsImZpbmFuY2lhbGNlbnRlciIsImFuYWx5emVkYXRhX21hbnVhbCIsImNhY2hlIiwiZ29vZHN3YXJlaG91c2UiLCJ0cmFkaW5nY2VudGVyIiwiY29tbW9ucHJvYmxlbSIsImludml0ZXBvc3RlciIsInNldHRvcCIsImV2ZXJ5ZGF5c2VsZWN0IiwiZXZlcnlkYXlob3QiLCJ1c2VyX3N0YXRlbWVudCIsImdvb2RzX21nciIsImdvb2RzbGlzdCIsInRvb2wiLCJub3N1Ym1pdCJdfQ.0Y7v6CHNJwK6Dbw4Z1famc21toqxVHRE2ax-zNDbnC0'
        // } ,
        success: res => {
          console.log(`${url} 返回结果`);
          console.log(res);

          if (res.statusCode === 200) {
            if (res.data.code == 0) {
              resolve(res.data);
            } else {
              console.log(res.data.code);
              console.log('===该请求失败.');
              let msg = "请求失败，请重试";

              if (res.data.msg != '') {
                msg = res.data.msg;
              }

              uni.showModal({
                title: '提示',
                content: msg,
                showCancel: false,
                complete: () => {
                  reject(msg);
                },

                success(res) {
                  if (res.confirm) {// wx.navigateBack({
                    //   delta: 1,
                    // })
                  } else if (res.cancel) {
                    console.log('用户点击取消');
                  }
                }

              });
              uni.hideLoading();
            }
          } else if (res.statusCode === 401) {
            getCode();
          } else {
            uni.showModal({
              title: '提示',
              content: res.errMsg,
              showCancel: false,
              complete: () => {
                reject(res.errMsg);
              },

              success(res) {
                if (res.confirm) {// wx.navigateBack({
                  //   delta: 1,
                  // })
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }

            });
            uni.hideLoading();
          }
        },
        fail: err => {
          if (err.errMsg === 'request:fail timeout') {// 超时
          }

          uni.showModal({
            title: '提示',
            content: err,
            showCancel: false,
            complete: () => {
              reject(err);
            },

            success(res) {
              if (res.confirm) {
                uni.navigateBack({
                  delta: 1
                });
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }

          });
          uni.hideLoading();
        }
      });
    });
  }
};